package sg.edu.nus.comp.icreate.ivletalk.announcement;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;

import sg.edu.nus.comp.icreate.ivletalk.utils.xml.AbstractXmlParser;

public class AnnouncementXmlParser extends AbstractXmlParser {
	private static final String TAG_DATA_ANNOUNCEMENT = "Data_Announcement";
	private static final String TAG_COURSE_CODE = "CourseCode";
	private static final String TAG_CREATED_DATE = "CreatedDate";
	private static final String TAG_NAME = "Name";
	private static final String TAG_DISCRIPTION = "Description";
	private static final String TAG_TITLE = "Title";
	private static final String TAG_IS_READ = "isRead";

	public AnnouncementXmlParser(InputStream inputStream) throws XmlPullParserException {
		super(inputStream);
	}

	public AnnouncementXmlParser() {
		super();
	}

	@Override
	public Object getResult() {
		List<Announcement> announcements = new ArrayList<Announcement>();
		int eventType;
		try {
			goToTag(TAG_RESULT, XmlPullParser.START_TAG);
			if (TAG_DATA_ANNOUNCEMENT.equals(parser.getName())) {
				do {
					eventType = parser.getEventType();
					if (eventType == XmlPullParser.START_TAG && TAG_DATA_ANNOUNCEMENT.equals(parser.getName())) {
						parser.next();
						if (eventType == XmlPullParser.START_TAG && TAG_COURSE_CODE.equals(parser.getName())) {
							getAnnouncement(announcements);
						}
					} else
						parser.next();
				} while (eventType != XmlPullParser.END_TAG || !TAG_RESULT.equals(parser.getName()));
			}
		} catch (Exception e) {
			Log.e("error", "Error in parse xml", e);
		}
		return announcements;
	}

	public void getAnnouncement(List<Announcement> announcements) {
		Announcement anmt = new Announcement();
		int eventType;
		try {
			do {
				eventType = parser.getEventType();
				if (eventType == XmlPullParser.START_TAG && TAG_CREATED_DATE.equals(parser.getName())) {
					parser.next();
					anmt.setCreatedDate(parser.getText());
				} else if (eventType == XmlPullParser.START_TAG && TAG_NAME.equals(parser.getName())) {
					parser.next();
					anmt.setCreator(parser.getText());
				} else if (eventType == XmlPullParser.START_TAG && TAG_DISCRIPTION.equals(parser.getName())) {
					parser.next();
					anmt.setDescription(parser.getText());
				} else if (eventType == XmlPullParser.START_TAG && TAG_TITLE.equals(parser.getName())) {
					parser.next();
					anmt.setTitle(parser.getText());
				} else if (eventType == XmlPullParser.START_TAG && TAG_IS_READ.equals(parser.getName())) {
					parser.next();
					anmt.setIsRead(parser.getText());
				} else
					parser.next();
			} while (eventType != XmlPullParser.END_TAG || !TAG_DATA_ANNOUNCEMENT.equals(parser.getName()));
		} catch (Exception e) {
			Log.e("error", "Error in get Announcement", e);
		}
		announcements.add(anmt);
	}

}
